import MusicManager from "../../MusicManager";
import BlackHoleGenerator from "../facility/priveteFacility/BlackHoleGenerator";
import { FacilityType } from "../info/GameEnum";
import { Global } from "../info/Global";
import PlayerManager from "../manager/PlayerManager";
import Dialog from "./Dialog";

const { ccclass, property } = cc._decorator;

@ccclass
export default class UpgradeSellDialog extends Dialog {

    @property(cc.Button)
    activate: cc.Button = null;

    @property(cc.Node)
    sell: cc.Node = null;

    @property(cc.Label)
    titleName: cc.Label = null;

    @property(cc.Label)
    explain: cc.Label = null;

    sellPrice: { diamond: number; oil: number; }
    facility: BlackHoleGenerator;

    public init(facility: BlackHoleGenerator) {
        this.facility = facility;
        let facilityInfo = Global.getFacilityInfo<{ 名字: string, 说明: string }>(FacilityType.黑洞发生器);
        this.titleName.string = facilityInfo.名字;
        this.explain.string = facilityInfo.说明;

        this.sellPrice = { diamond: Math.ceil(facility.price.diamond / 2), oil: Math.ceil(facility.price.oil / 2) };
        let sellDiamond = cc.find("price/diamond", this.sell);
        let sellOil = cc.find("price/oil", this.sell);
        sellDiamond.active = !!this.sellPrice.diamond;
        sellOil.active = !!this.sellPrice.oil;
        sellDiamond.active && (sellDiamond.getComponentInChildren(cc.Label).string = this.sellPrice.diamond.toString());
        sellOil.active && (sellOil.getComponentInChildren(cc.Label).string = this.sellPrice.oil.toString());

        this.activate.interactable = !facility.hole.active && !facility.creating;
    }


    onClick(event: cc.Event) {
        MusicManager.instance.play(MusicManager.instance.btnMove);
        let player = PlayerManager.instance.controllerPlayer
        let name = event.target.name;
        switch (name) {
            case "激活":
                this.facility.run();
                break;
            case "卖出":
                player.diamond += this.sellPrice.diamond;
                player.oil += this.sellPrice.oil;
                this.facility.die(null);
                break;
        }
        this.closeDialog();
    }

    protected update(dt: number): void {
        this.activate.interactable = !this.facility.detectionCollider.enabled && this.facility.hole.active
    }
}
